﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class product_insurance_admin_applicant_details_accident : System.Web.UI.Page
{
    ApplicantDetailsAccidentBLL bll = new ApplicantDetailsAccidentBLL(); 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int id = int.Parse(Request.QueryString["applicationID"]);
            bll.applicationID = id;
            this.applicationID.Value = Convert.ToString(id);

            DataSet self = bll.getSelfDetail();
            this.Self_FormView.DataSource = bll.getSelfDetail();
            this.Self_FormView.DataBind();

            string custId = self.Tables[0].Rows[0]["custID"].ToString();
            this.custId.Value = custId;

            this.Relation_Repeater.DataSource = bll.getFamilyDetail();
            this.Relation_Repeater.DataBind();

            Hashtable spouseDetail = bll.getSpouseDetail();
            this.lbs_occupation.Text = spouseDetail["occupation"] as String;

            Employer_FormView.DataSource = bll.getSelfDetail();
            Employer_FormView.DataBind();

            this.Illness_Repeater.DataSource = bll.getIllnessList();
            this.Illness_Repeater.DataBind();

            this.Hazardous_Repeater.DataSource = bll.getHazardousList();
            this.Hazardous_Repeater.DataBind();

            this.TravelFrequency_Repeater.DataSource = bll.getTravelFrequencyList();
            this.TravelFrequency_Repeater.DataBind();

            this.DeclinedHistory_Repeater.DataSource = bll.getDeclinedHistory();
            this.DeclinedHistory_Repeater.DataBind();

            DateTime startDt = (DateTime)self.Tables[0].Rows[0]["startDate"];
            DateTime endDt = (DateTime)self.Tables[0].Rows[0]["endDate"];

            this.tbStartDate.Text = startDt.ToString("dd/MM/yyyy");
            this.tbEndDate.Text = endDt.ToString("dd/MM/yyyy");

            string paymentPeriod = self.Tables[0].Rows[0]["paymentPeriod"].ToString();
            this.lb_paymentPeriod.Text = paymentPeriod;

            this.Plan_Repeater.DataSource = bll.getPlanList();
            this.Plan_Repeater.DataBind();

        }
    }
    protected void btngenerate_certificate_Click(object sender, EventArgs e)
    {


        LinkedList<int> levels = new LinkedList<int>();
        LinkedList<int> insuredIds = new LinkedList<int>();

        int count = this.Plan_Repeater.Items.Count;
        for (int i = 0; i < count; i++)
        {
            CheckBox cb_accept = (CheckBox)this.Plan_Repeater.Items[i].FindControl("cb_accept");
            DropDownList ddl = (DropDownList)this.Plan_Repeater.Items[i].FindControl("ddlPremiumLevel");
            HiddenField hf_insuredId = (HiddenField)this.Plan_Repeater.Items[i].FindControl("hf_insuredId");

            if (cb_accept.Checked)
            {
                int selectValue = int.Parse(ddl.SelectedValue);
                levels.AddLast(selectValue);
                insuredIds.AddLast(int.Parse(hf_insuredId.Value));
            }

        }

        var approval = new ApprovalBLL
        {
            CustID = this.custId.Value,
            StaffID = (String)Session["userId"],
            ApplicationInsuranceId = int.Parse(this.applicationID.Value),
            Type = "INSURANCE",
            Category = "A",
            StartDate = Convert.ToDateTime(this.tbStartDate.Text),
            EndDate = Convert.ToDateTime(this.tbEndDate.Text)
        };

        approval.levels = levels;
        approval.applyInsuredIds = insuredIds;

        approval.approved();
        /* 
        var approval = new Approval
        {
            CustID=this.custId.Value,
            StaffID = "111523658",//(String)Session["userId"],
            ApplicationInsuranceId = int.Parse(this.applicationID.Value),
            Type="INSURANCE",
            Category = "H",
            StartDate = Convert.ToDateTime(this.tbStartDate.Text),
            EndDate = Convert.ToDateTime(this.tbEndDate.Text)
        };

        approval.approved();*/

       this.Response.Redirect("./insurance_applicant_list.aspx");
    }
}